/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import model.MateriaPrima;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author Fran
 */
public class MateriaPrimaDAO {
    
     public MateriaPrimaDAO() {
        
        session = HibernateUtil.getSessionFactory().openSession();
        
    }
     
     private Session session;
     
     
     public void inserir(MateriaPrima materiaprima)
    {
        
        Transaction t = session.beginTransaction();
        session.save(materiaprima);
        t.commit();
        
    }
    
    public List<MateriaPrima> findAll()
    {
    
        return session.createQuery("from MateriaPrima").list();
    
    }
    
    public MateriaPrima findById(int id)
    {
    
        return (MateriaPrima) session.load(MateriaPrima.class, id);
        
    }
    
    public void update(MateriaPrima materiaprima){
        
        Transaction t = session.beginTransaction();
        session.merge(materiaprima);
        t.commit();       
        
    }
    
    public List<MateriaPrima>findAllMatPrima(String desc){
        Query qu = session.createQuery("From MateriaPrima where descricao like '%"+desc+"%'");
        return qu.list();
    }
    
}
